#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "func.h"

void	func_gen_innerc(func_t *func, char *out)
{
	int	i, n = 0;
	PARAM	*pa;

	sprintf(out, "%s\t", func->return_type.name);

	n = strlen(out);
	sprintf(out + n ,"%s_inner\n", func->name);

	strcat(out, "(\n");

	if(func->pa_num > 1)
	{
		strcat(out, "\tvoid *ctx,\n");
	}
	else
	{
		strcat(out, "\tvoid *ctx\n");
	}
	for(i = 1; i < func->pa_num; i++)
	{
		pa = func->pa + i;
		n = strlen(out);
		sprintf(out + n, "\t%s ", pa->type.name);

		n = strlen(out);
		if(i != func->pa_num - 1)
		{
			sprintf(out + n, "%s,\n", pa->name);
		}
		else
		{
			sprintf(out + n, "%s\n", pa->name);
		}
	}

	strcat(out, ")\n");
	strcat(out, "{\n");

	if(func->code == NULL)
	{
		strcat(out, "\treturn CKR_OK;\n");
	}
	else
	{
		strcat(out, func->code);
	}
	strcat(out, "}");
}
